package com.ruoyi.framework.config;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.strategy.SaAnnotationStrategy;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.config.properties.PermitAllUrlProperties;
import com.ruoyi.framework.security.context.PermissionContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

//    @Autowired
//    private TokenService tokenService;

//    @Autowired
//    private ServerConfig serverConfig;

    // 注册sa-token的拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册路由拦截器，自定义验证规则
        registry.addInterceptor(
                new SaInterceptor((handler) -> {
                    PermitAllUrlProperties properties = SpringUtils.getBean(PermitAllUrlProperties.class);
                    SaRouter.match(List.of("/**"))
                            // 排除允许匿名访问的地址
                            .notMatch(properties.getUrls())
                            // 排除下不需要拦截的
                            .notMatch(Arrays.asList(
                                    "/login",
                                    "/logout",
                                    "/register",
                                    "/captchaImage",
                                    "/*.html",
                                    "/*/*.html",
                                    "/*/*.css",
                                    "/*/*.js",
                                    "/profile/**",
                                    "/common/download**",
                                    "/common/download/resource**",
                                    "/doc.html",
                                    "/swagger-resources/**",
                                    "/webjars/**",
                                    "/v3/api-docs/**",
                                    "/druid/**"))
                            .check(() -> {
                                // 检查是否登录 是否有token
                                StpUtil.checkLogin();

//                                long tokenTimeout = StpUtil.getTokenTimeout();
//                                long sessionTimeout = StpUtil.getSessionTimeout();
//                                long tokenSessionTimeout = StpUtil.getTokenSessionTimeout();
//                                log.info("tokenSessionTimeout: {}, tokenTimeout: {}, sessionTimeout: {}", tokenSessionTimeout, tokenTimeout, sessionTimeout);

                                //  StpUtil.renewTimeout(StpUtil.getStpLogic().getConfigOrGlobal().getTimeout());
                            });

                    if (handler instanceof HandlerMethod) {
                        Method method = ((HandlerMethod)handler).getMethod();
                        SaCheckPermission checkPermission = method.getAnnotation(SaCheckPermission.class);
                        if (checkPermission != null) {
                            PermissionContextHolder.setContext(checkPermission.value());
                        }
                    }

                })
        ).addPathPatterns("/**");
    }

}
